﻿using System;
using Autofac.Core;
using Autofac.Core.Registration;

namespace Autofac.Integration.Prism
{

	///<summary>
	///</summary>
	public class ChildContainer: Container, IChildContainer
	{
		readonly IContainer _parentContainer;

		public static IChildContainer Create(IContainer parentContainer, Action<ContainerBuilder> configurationAction)
		{
			if (parentContainer == null) throw new ArgumentNullException("parentContainer");

			// This will create a ScopeRestrictedRegistry containing the registration
			// info for all higher levels.
			var rootLifetimeScope = parentContainer.BeginLifetimeScope(configurationAction ?? (builder => {}));
			var componentRegistry = (ComponentRegistry) rootLifetimeScope.ComponentRegistry;

			return new ChildContainer(parentContainer, componentRegistry, rootLifetimeScope);
		}

		ChildContainer(IContainer parentContainer, ComponentRegistry componentRegistry, ILifetimeScope rootLifetimeScope): base(componentRegistry, rootLifetimeScope)
		{
			_parentContainer = parentContainer;
		}

		///<summary>
		///</summary>
		public IContainer ParentContainer
		{
			get { return _parentContainer; }
		}
	}
}